package com.vegetable.modules.entity.outbound;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.vegetable.modules.entity.stock.Stock;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售订货任务下达表
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="SalesOrderTransmit对象", description="销售订货任务下达表")
public class SalesOrderTransmit  {

    @ApiModelProperty("主键id")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    @ApiModelProperty(value = "生产者ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long producerId;

    @ApiModelProperty(value = "商品ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long commodityId;

    @ApiModelProperty(value = "商品编码")
    @TableField(exist = false)
    private String commodityCode;

    @ApiModelProperty(value = "商品分类")
    @TableField(exist = false)
    private String category;

    @ApiModelProperty(value = "商品名称")
    private String commodityName;

    @ApiModelProperty(value = "规格ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long specsId;

    @ApiModelProperty(value = "规格名称")
    private String specsName;

    @ApiModelProperty(value = "标准")
    private BigDecimal standard;

    @ApiModelProperty(value = "数量")
    private Integer number;

    @ApiModelProperty(value = "重量")
    private BigDecimal weight;

    @ApiModelProperty(value = "下达数量")
    private Integer completeNumber = 0;

    @ApiModelProperty(value = "下达重量")
    private BigDecimal completeWeight = BigDecimal.ZERO;

    @ApiModelProperty(value = "未下达数量")
    private Integer surplusNumber;

    @ApiModelProperty(value = "未下达重量")
    private BigDecimal surplusWeight;

    @ApiModelProperty(value = "库存数量")
    @TableField(exist = false)
    private Integer stockNumber;

    @ApiModelProperty(value = "库存重量")
    @TableField(exist = false)
    private BigDecimal stockWeight;

    public void setStocks(List<Stock> stocks) {
        if(CollectionUtil.isNotEmpty(stocks)){
            Map<String, List<Stock>> listMap = stocks.stream().collect(Collectors.groupingBy(r -> r.getWarehouseName()));
            List<Stock> tem = new ArrayList<>();
            for(Map.Entry<String, List<Stock>> en : listMap.entrySet()){
                Stock stock = new Stock();
                stock.setWarehouseName(en.getKey());
                stock.setNumber(en.getValue().stream().mapToInt(e -> e.getNumber()).sum());
                stock.setWeight(en.getValue().stream().map(e -> e.getWeight()).reduce(BigDecimal.ZERO, BigDecimal::add));
                tem.add(stock);
            }
            this.stocks = tem;
        }else{
            this.stocks = stocks;
        }
    }

    @ApiModelProperty(value = "库存详情")
    @TableField(exist = false)
    private List<Stock> stocks;

    @ApiModelProperty(value = "下达详情")
    @TableField(exist = false)
    private List<Map<String, String>> completes;

    @ApiModelProperty(value = "备注")
    private String remark;
    @TableField(exist = false)
    private List<Map<String, Object>> remarkDetail;
}
